package com.lkwlw.tplms.repertory.action;

import java.util.List;
import java.util.Map;

import com.lkwlw.tplms.order.dao.mapper.OrderinfoMapper;
import com.lkwlw.tplms.order.dao.model.Orderinfo;
import com.lkwlw.tplms.repertory.dao.mapper.InventoryinfoMapper;
import com.lkwlw.tplms.repertory.dao.mapper.WarehouseinfoMapper;
import com.lkwlw.tplms.repertory.dao.model.Inventoryinfo;
import com.lkwlw.tplms.repertory.dao.model.InventoryinfoExample;
import com.lkwlw.tplms.repertory.dao.model.Warehouseinfo;
import com.lkwlw.tplms.repertory.dao.model.WarehouseinfoExample;
import com.lkwlw.tplms.repertory.dao.model.WarehouseinfoExample.Criteria;
import com.lkwlw.tplms.system.dao.model.Admininfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class QueryStockAction extends ActionSupport {
	private static final long serialVersionUID = 5838403027616193182L;
	private InventoryinfoMapper inventoryinfoMapper;
	private WarehouseinfoMapper warehouseinfoMapper;
	private OrderinfoMapper orderInfoDAO;
	private Inventoryinfo inventoryinfo;	
	private Warehouseinfo warehouseinfo;
	private Orderinfo order;
	private List<Warehouseinfo> warehouseList;
	private List<Map<String,Object>> stockOutOrderList;	
	private String orderid;
	private String errorMessage;
	
	public String execute() throws Exception {
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();
		Admininfo admin = (Admininfo) session.get("admin");
		if (null != admin) {
			WarehouseinfoExample example = new WarehouseinfoExample();
			Criteria criteria = example.createCriteria();
			criteria.andWhkeeperidEqualTo(admin.getId());
			warehouseList = warehouseinfoMapper.selectByExample(example);
			if (null != warehouseList && warehouseList.size() > 0) {
				warehouseinfo = warehouseList.get(0);
				if (!session.containsKey("warehouse")) {
					session.put("warehouse", warehouseList.get(0));
				}
			}	
		}		
		return SUCCESS;
	}
	
	public String queryOrder() throws Exception{
		order = orderInfoDAO.selectByPrimaryKey(orderid);
		errorMessage = "";
		if (null == order) {
			errorMessage = "norecord";
		}else{
			ActionContext context = ActionContext.getContext();
			Map session = context.getSession();
			Admininfo admin = (Admininfo) session.get("admin");
			if (null != admin && null != orderid && !"".equals(orderid)) {			
				if (session.containsKey("warehouse")) {
					warehouseinfo = (Warehouseinfo) session.get("warehouse");
				}else{
					WarehouseinfoExample example = new WarehouseinfoExample();
					Criteria criteria = example.createCriteria();
					criteria.andWhkeeperidEqualTo(admin.getId());
					warehouseList = warehouseinfoMapper.selectByExample(example);
					warehouseinfo = warehouseList.get(0);
				}
				InventoryinfoExample example = new InventoryinfoExample();
				com.lkwlw.tplms.repertory.dao.model.InventoryinfoExample.Criteria criteria =
						example.createCriteria();
				criteria.andOrderidEqualTo(orderid);
				criteria.andWarehouseidEqualTo(warehouseinfo.getId());
				criteria.andStockinoutEqualTo(2);
				List<Inventoryinfo> stockOutList = inventoryinfoMapper.selectByExample(example);
				if (null != stockOutList && 0 < stockOutList.size()) {
					errorMessage = "duplicate_stockout";	
					inventoryinfo = inventoryinfoMapper.selectByExample(example).get(0);
				}else {
					inventoryinfo.setId(null);
					inventoryinfo.setOrderid(orderid);
					inventoryinfo.setStockinout(2);
					inventoryinfo.setWarehouseid(warehouseinfo.getId());
					inventoryinfo.setRegistrant(admin.getFullName());
					inventoryinfo.setRemark("订单已出" + warehouseinfo.getWarehouse() + "仓库");
					inventoryinfoMapper.insertSelective(inventoryinfo);
					Orderinfo orderinfo =  new Orderinfo();
					orderinfo.setId(orderid);
					orderinfo.setState("已出库");
					orderInfoDAO.updateStateByPrimaryKey(orderinfo);
					//example.setOrderByClause("registerDate desc");
					inventoryinfo = inventoryinfoMapper.selectByExample(example).get(0);
					errorMessage = "success";
				}
			}			
		}
		execute();
		orderid = "";
		return SUCCESS;
	}
	
	public String stockOut() throws Exception {
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();
		Admininfo admin = (Admininfo) session.get("admin");
		if (null != admin && null != orderid && !"".equals(orderid)) {			
			if (session.containsKey("warehouse")) {
				warehouseinfo = (Warehouseinfo) session.get("warehouse");
			}else{
				WarehouseinfoExample example = new WarehouseinfoExample();
				Criteria criteria = example.createCriteria();
				criteria.andWhkeeperidEqualTo(admin.getId());
				warehouseList = warehouseinfoMapper.selectByExample(example);
				warehouseinfo = warehouseList.get(0);
			}
			inventoryinfo.setOrderid(orderid);
			inventoryinfo.setStockinout(2);
			inventoryinfo.setWarehouseid(warehouseinfo.getId());
			inventoryinfo.setRegistrant(admin.getFullName());
			inventoryinfo.setRemark("订单已出" + admin.getArea() + "仓库");
			inventoryinfoMapper.insertSelective(inventoryinfo);
			Orderinfo orderinfo =  new Orderinfo();
			orderinfo.setId(orderid);
			orderinfo.setState("已出库");
			orderInfoDAO.updateStateByPrimaryKey(orderinfo);
			InventoryinfoExample example = new InventoryinfoExample();
			com.lkwlw.tplms.repertory.dao.model.InventoryinfoExample.Criteria criteria =
					example.createCriteria();
			criteria.andOrderidEqualTo(orderid);
			criteria.andWarehouseidEqualTo(warehouseinfo.getId());
			criteria.andStockinoutEqualTo(2);
			example.setOrderByClause("registerDate desc");
			inventoryinfo = inventoryinfoMapper.selectByExample(example).get(0);
		}
		execute();
		return SUCCESS;
	}
	public String cancelStockOut() throws Exception{
		errorMessage = "";
		execute();
		return SUCCESS;
	}
	public void setInventoryinfoMapper(InventoryinfoMapper inventoryinfoMapper) {
		this.inventoryinfoMapper = inventoryinfoMapper;
	}

	public void setWarehouseinfoMapper(WarehouseinfoMapper warehouseinfoMapper) {
		this.warehouseinfoMapper = warehouseinfoMapper;
	}

	public void setInventoryinfo(Inventoryinfo inventoryinfo) {
		this.inventoryinfo = inventoryinfo;
	}
	
	public Inventoryinfo getInventoryinfo() {
		return inventoryinfo;
	}

	public Warehouseinfo getWarehouseinfo() {
		return warehouseinfo;
	}
	
	public void setWarehouseinfo(Warehouseinfo warehouseinfo) {
		this.warehouseinfo = warehouseinfo;
	}

	public List<Warehouseinfo> getWarehouseList() {
		return warehouseList;
	}

	public void setWarehouseList(List<Warehouseinfo> warehouseList) {
		this.warehouseList = warehouseList;
	}
	public List<Map<String, Object>> getStockOutOrderList() {
		return stockOutOrderList;
	}

	public void setStockOutOrderList(List<Map<String, Object>> stockOutOrderList) {
		this.stockOutOrderList = stockOutOrderList;
	}

	public String getOrderid() {
		return orderid;
	}

	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}

	public void setOrderInfoDAO(OrderinfoMapper orderInfoDAO) {
		this.orderInfoDAO = orderInfoDAO;
	}

	public Orderinfo getOrder() {
		return order;
	}

	public void setOrder(Orderinfo order) {
		this.order = order;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

}
